const cheerio = require('cheerio');
const http = require('http');
const iconv = require('iconv-lite');
const fs = require('fs');
// 引入数据库文件
const db = require("./db");
// 设置需要抓取的网页
var url = 'http://www.jobbole.com/';

http.get(url, function (sres) {
  var chunks = [];
  sres.on('data', function (chunk) {
    chunks.push(chunk);
  });
  sres.on('end', function () {
    // 设置编码
    var html = iconv.decode(Buffer.concat(chunks), 'utf-8');
    // 使用类似jq的选择器
    var $ = cheerio.load(html, { decodeEntities: false });
    $('#widgets-homepage-fullwidth > div:nth-child(3) .grid-4').each((idx, element) => {
      var $element = $(element);
      $element.find('.floated-thumb').each(function (index, ele) {
        var imgUrl = $(ele).find('.post-thumb > a > img').attr('src');
        var title = $(ele).find('.post-meta .meta-title').text();
        // 插入到数据库中
        db('insert into new(title, img_url) values(?,?)', [title, imgUrl], (err, data) => {
          if (err) {
            console.log('错误了');
            return false;
          }
          if (data) {
            console.log('成功插入到mysql中');
          }
        });
      });
    });
  });
});